package org.framework.customutil;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

/**
 * @Title: BrowserUtils.java .<br>
 * @Package org.framework.customutil .<br>
 * @Description: 浏览器工具类 .<br>
 * @author 郑成功 .<br>
 * @email a876459952@qq.com .<br>
 * @date 2018-6-7 下午8:37:38.<br>
 * @version V1.0.<br>
 */
public class BrowserUtils{
	
	private final static String IE11 = "rv:11.0";
	private final static String IE10 = "MSIE 10.0";
	private final static String IE9 = "MSIE 9.0";
	private final static String IE8 = "MSIE 8.0";
	private final static String IE7 = "MSIE 7.0";
	private final static String IE6 = "MSIE 6.0";
	private final static String MAXTHON = "Maxthon";
	private final static String QQ = "QQBrowser";
	private final static String GREEN = "GreenBrowser";
	private final static String SE360 = "360SE";
	private final static String FIREFOX = "Firefox";
	private final static String OPERA = "Opera";
	private final static String CHROME = "Chrome";
	private final static String SAFARI = "Safari";
	private final static String OTHER = "其它";
	
	private static Map<String, String> langMap = new HashMap<String, String>();
	private final static String ZH = "zh";
	private final static String ZH_CN = "zh-cn";

	private final static String EN = "en";
	private final static String EN_US = "en";

	static{
		langMap.put(ZH, ZH_CN);
		langMap.put(EN, EN_US);
	}

	/**
	 * @Description: 判断是否是IE .<br>
	 * @author 郑成功 .<br>
	 * @date 2018-6-7 下午8:38:25.<br>
	 */
	public static boolean isIE(HttpServletRequest request){
		return (request.getHeader("USER-AGENT").toLowerCase().indexOf("msie") > 0 || request.getHeader("USER-AGENT").toLowerCase().indexOf("rv:11.0") > 0) ? true : false;
	}

	/**
	 * @Description: 获取IE版本 .<br>
	 * @author 郑成功 .<br>
	 * @date 2018-6-7 下午8:38:42.<br>
	 */
	public static Double getIEversion(HttpServletRequest request){
		Double version = 0.0;
		if (getBrowserType(request, IE11)){
			version = 11.0;
		}else if (getBrowserType(request, IE10)){
			version = 10.0;
		}else if (getBrowserType(request, IE9)){
			version = 9.0;
		}else if (getBrowserType(request, IE8)){
			version = 8.0;
		}else if (getBrowserType(request, IE7)){
			version = 7.0;
		}else if (getBrowserType(request, IE6)){
			version = 6.0;
		}
		return version;
	}

	/**
	 * @Description: 获取浏览器类型 .<br>
	 * @author 郑成功 .<br>
	 * @date 2018-6-7 下午8:39:30.<br>
	 */
	public static BrowserType getBrowserType(HttpServletRequest request){
		BrowserType browserType = null;
		if (getBrowserType(request, IE11)){
			browserType = BrowserType.IE11;
		}
		if (getBrowserType(request, IE10)){
			browserType = BrowserType.IE10;
		}
		if (getBrowserType(request, IE9)){
			browserType = BrowserType.IE9;
		}
		if (getBrowserType(request, IE8)){
			browserType = BrowserType.IE8;
		}
		if (getBrowserType(request, IE7)){
			browserType = BrowserType.IE7;
		}
		if (getBrowserType(request, IE6)){
			browserType = BrowserType.IE6;
		}
		if (getBrowserType(request, FIREFOX)){
			browserType = BrowserType.Firefox;
		}
		if (getBrowserType(request, SAFARI)){
			browserType = BrowserType.Safari;
		}
		if (getBrowserType(request, CHROME)){
			browserType = BrowserType.Chrome;
		}
		if (getBrowserType(request, OPERA)){
			browserType = BrowserType.Opera;
		}
		if (getBrowserType(request, "Camino")){
			browserType = BrowserType.Camino;
		}
		return browserType;
	}

	/**
	 * @Description: 检测浏览器类型 .<br>
	 * @param brosertype 比较类型 .<br>
	 * @author 郑成功 .<br>
	 * @date 2018-6-7 下午8:40:35.<br>
	 */
	private static boolean getBrowserType(HttpServletRequest request, String brosertype){
		return request.getHeader("USER-AGENT").toLowerCase().indexOf(brosertype) > 0 ? true : false;
	}

	/**
	 * @Description: 检测访问的浏览器 .<br>
	 * @author 郑成功 .<br>
	 * @date 2018-6-7 下午8:41:45.<br>
	 */
	public static String checkBrowse(HttpServletRequest request){
		String userAgent = request.getHeader("USER-AGENT");
		if (regex(OPERA, userAgent))
			return OPERA;
		if (regex(CHROME, userAgent))
			return CHROME;
		if (regex(FIREFOX, userAgent))
			return FIREFOX;
		if (regex(SAFARI, userAgent))
			return SAFARI;
		if (regex(SE360, userAgent))
			return SE360;
		if (regex(GREEN, userAgent))
			return GREEN;
		if (regex(QQ, userAgent))
			return QQ;
		if (regex(MAXTHON, userAgent))
			return MAXTHON;
		if (regex(IE11, userAgent))
			return IE11;
		if (regex(IE10, userAgent))
			return IE10;
		if (regex(IE9, userAgent))
			return IE9;
		if (regex(IE8, userAgent))
			return IE8;
		if (regex(IE7, userAgent))
			return IE7;
		if (regex(IE6, userAgent))
			return IE6;
		return OTHER;
	}

	/**
	 * @Description: 正则验证 .<br>
	 * @param regex 正则表达式.<br>
	 * @param str 验证的字符串.<br>
	 * @author 郑成功 .<br>
	 * @date 2018-6-7 下午8:42:22.<br>
	 */
	private static boolean regex(String regex, String str){
		Pattern p = Pattern.compile(regex, Pattern.MULTILINE);
		Matcher m = p.matcher(str);
		return m.find();
	}

	/**
	 * @Description: 获取浏览器的语言 .<br>
	 * @author 郑成功 .<br>
	 * @date 2018-6-7 下午8:43:28.<br>
	 */
	public static String getBrowserLanguage(HttpServletRequest request){

		String browserLang = request.getLocale().getLanguage();
		String browserLangCode = (String) langMap.get(browserLang);

		if (browserLangCode == null)
		{
			browserLangCode = EN_US;
		}
		return browserLangCode;
	}

}
